package org.protobuf;

import org.protobuf.entity.PersonEntity;
import org.protobuf.entity.PersonEntity.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.protobuf.InvalidProtocolBufferException;

/**
 * @author donald
 * 2018年3月12日
 * 上午9:26:16
 */
public class ProtobufBoot {
	private static final Logger log = LoggerFactory.getLogger(ProtobufBoot.class);

	public static void main(String[] args) throws InvalidProtocolBufferException{
		//模拟将对象转成byte[]，方便传输
        PersonEntity.Person.Builder builder = PersonEntity.Person.newBuilder();
        builder.setAge(25);
        builder.setName("donald");
        builder.setEmail("shaoqinghan@aliyun.com");
        PersonEntity.Person person = builder.build();
        log.info("build persion :{}"+ person.toString());
        log.info("===========Person Serialize Byte==========");
        for(byte b : person.toByteArray()){
            log.info("Person Serialize Byte:{}",b);
        }
        log.info("Person Serialize ByteString:{}",person.toByteString().toString());

        //模拟接收Byte[]，反序列化成Person类
        byte[] byteArray =person.toByteArray();
        Person dePerson = Person.parseFrom(byteArray);
        log.info("Deserialize  Person:" +dePerson.toString());
	}
	
}
